#!/usr/bin/env python  
# encoding: utf-8


""" 
@version: v1.0 
@author: Shylock 
@license: Apache Licence  
@contact: JYFelt@163.com 
@site: http://blog.csdn.net/weixin_38034182 
@software: PyCharm Community Edition 
@file: MYSQL_CL.py 
@time: 2017/12/23 22:29 
"""
import MySQLdb as mdb

sql = ''
list__1 = []
'''连接到数据库'''


def __connect(open):
    con = None
    global cur
    if open == 1:
        try:
            # 连接 mysql 的方法：connect('ip','user','password','databasename','charset')
            con = mdb.connect('localhost', 'root', '*****', 'zhiye', charset='utf8')
            # 建立查询于cursor模块上
            cur = con.cursor()
            return cur
        except mdb as e:
            print(e)
    if open == 0:
        if con:
            con.close()


def __exec(sql):
    # 连接数据库
    __connect(1)

    # 执行语句:
    cur.execute(sql)

    # 取得上个查询的结果，是单个结果
    data = (cur.fetchall())

    # 断开数据库
    __connect(0)
    list__1 = list(data)
    # 返回data值以来调用
    for i in data:
        for j in range(len(data)):
            list__1[j] = (i)




# SHOW_VERSION = __exec(sql='SELECT VERSION()')  # 返回当前数据库版本
# FIND_BY_SQL = __exec(sql='SELECT * FROM zhiyefenlei')  # 根据sql查找
FIND_BY_SQL_J = __exec(sql='SELECT * from test')
COUNT_BY_SQL = "countBySql"  # 自定义sql 统计影响行数
INSERT = "insert"  # 插入
UPDATE_BY_ATTR = "updateByAttr"  # 更新数据
DELETE_BY_ATTR = "deleteByAttr"  # 删除数据
FIND_BY_ATTR = "findByAttr"  # 根据条件查询一条记录
FIND_ALL_BY_ATTR = "findAllByAttr"  # 根据条件查询多条记录
COUNT = "count"  # 统计行
EXIST = "exist"  # 是否存在该记录

print(FIND_BY_SQL_J)
